/**
 * 全局访问路径
 */
var path = "http://127.0.0.1:8899/api/consumer/"
/**
 * 图片全局路径
 */
var imgs = "http://120.48.47.144:8899/images/";
/**
 * 页面全局路径
 */
var pathHtml = "http://127.0.0.1:8899/html/";
var pages = 0; //总页数
var pageRankingIndex = 1; //当前页数
var load_leng = 1500; //指定高度加载一次
var brand_id=0;   //品牌id
var car_level_id=0;   //等级id
var sales_sell_date=0;   //销售日期
var min_price=0;   //最低金额
var max_price=0;   //最高金额

$(function() {
	/* 根据token获取用户信息 */
	get_user_mess_and_token();
	//自己代码
	//查询所有等级
	car_level();
	frist_letter_brand_all();
	//排行榜查询所有(根据销量排行)
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
	//排行榜(前十热销榜)
	hot_ranking();
	//排行榜(前十降价榜)
	cut_price_ranking();
	timeAll();
	backVertex();
});

//**************排行榜(热销榜)**************************
function hot_ranking(){
	$.post(path+'Car_seriesController/select_hot_ranking',function(data){
		var hot="";
		$(data).each(function(index,item){
			hot+="<li class='hot-sales-rank_item__UZthc'>"
			if(index==0){
				hot+="<a class='hot-sales-rank_link__3KvJ7 common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank-1-ad693ae7b062b325b797..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'>"+
				"<span class='hot-sales-rank_item-rank__1r1pX hot-sales-rank_item-rank-1__2NyHq'>1</span>"
			}else if(index==1){
				hot+="<a class='hot-sales-rank_link__3KvJ7 common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank-1-ad693ae7b062b325b797..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'> "+
				"<span class='hot-sales-rank_item-rank__1r1pX hot-sales-rank_item-rank-2__2UhWX'>2</span>"
			}else if(index==2){
				hot+="<a class='hot-sales-rank_link__3KvJ7 common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank-1-ad693ae7b062b325b797..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'> "+
				"<span class='hot-sales-rank_item-rank__1r1pX hot-sales-rank_item-rank-3__1SSuh'>3</span>"
			}else{
				hot+="<a class='hot-sales-rank_link__3KvJ7 common-link_root__2VBT4' style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank-normal-31ac2f8c5d8fa701d3c8..png)' target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"' rel='opener'>"+
				"<span class='hot-sales-rank_item-rank__1r1pX'>"+(index+1)+"</span>"
			}
			hot+="<span class='hot-sales-rank_item-info__z73-K'>"+
			"<strong class='hot-sales-rank_item-info-title__JSnDI line-1'>"+item.car_series_name+"</strong>"+
			"<span class='hot-sales-rank_item-info-price__2Gbyg'>"+item.min_manufacturer_price+"-"+item.max_manufacturer_price+"万</span></span>"+
			"<span class='hot-sales-rank_item-sales__134Ig'>"+
			"<span class='hot-sales-rank_item-sales-volume__19dk3'>"+commaSeparateNumber(item.sales.sales_count)+"</span>"+
			"</span></a>"+
			"</li>"
		});
		$("#hot_ranking li").remove();
		$("#hot_ranking").append(hot);
	},"json");
	
}
/* <span class='hot-sales-rank_item-sales-tag__3en1d'>本地销量</span> */
//**************排行榜(降价榜)**************************
function cut_price_ranking(){
	$.post(path+'Car_seriesController/select_cut_price_ranking',function(data){
		var cut_price ="";
		var ratio_cut="";   //降幅比率
		$(data).each(function(index,item){
			//最高金额/(最低金额+降幅);
			var ratio=Math.round((parseInt(item.car_model.car_model_max_decline)/parseInt(item.min_manufacturer_price+item.car_model.car_model_max_decline))*100);
			ratio_cut=ratio;
			cut_price+="<li class='reduce-price-rank_item__1BJvF'>"
			if(index==0){
				cut_price+="<a class='reduce-price-rank_link__38yjP common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/reduce-1-f186d26b2c896d404397..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'>"+
				"<span class='reduce-price-rank_item-rank__1EnMX reduce-price-rank_item-rank-1__vLn-l'>1</span>"
			}else if(index==1){
				cut_price+="<a class='reduce-price-rank_link__38yjP common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/reduce-1-f186d26b2c896d404397..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'>"+
				"<span class='reduce-price-rank_item-rank__1EnMX reduce-price-rank_item-rank-2__ANAEX'>2</span>"
			}else if(index==2){
				cut_price+="<a class='reduce-price-rank_link__38yjP common-link_root__2VBT4' "+
			"style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/reduce-1-f186d26b2c896d404397..png)' "+
			"target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"'>"+
				"<span class='reduce-price-rank_item-rank__1EnMX reduce-price-rank_item-rank-3__2KX0P'>3</span>"
			}else{
				cut_price+="<a class='reduce-price-rank_link__38yjP common-link_root__2VBT4' style='background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/reduce-normal-1675406477803b61e95c..png)' target='_blank' href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"' rel='opener'>"+
				"<span class='reduce-price-rank_item-rank__1EnMX'>"+(index+1)+"</span>"
			}
			cut_price+="<span class='reduce-price-rank_item-info__2Ks7F'>"+
			"<span class='reduce-price-rank_info-title__1J1pF line-1'>"+item.car_series_name+"</span>"+
			"<span class='reduce-price-rank_info-price__1hyXp'>"+item.min_manufacturer_price+"-"+item.max_manufacturer_price+"万"+
			"<span class='reduce-price-rank_info-reduce__CvR2r'>"+
			"<i class='DCD_Icon icon_reduce'></i>"+item.car_model.car_model_max_decline+"万</span></span>"+
			"<span class='reduce-price-rank_info-rate__1MFIx'>最大降幅"+ratio_cut+"%</span></span>"+
			"</a>"+
			"</li>"
		});
		$("#cut_price_ranking li").remove();
		$("#cut_price_ranking").append(cut_price);
	},"json");
	
}

//************** 排行榜查询所有的等级 **************************
function car_level(){
	$.post(path+'Car_levelController/select_all_level',function(data){
		var level_str="";
		level_str+="<div class='levels_level__3XKgb' id='level_all' >"+
			"<div class='tw-relative levels_button__oiTOw tw-mr-8 levels_active__3usSw levels_large__LbzbP'>"+
			"<a  value='0' onclick='changelevelAll(this)'>全部</a>"+
			"</div>"
		$(data).each(function(index,item){
			level_str+="<div class='tw-relative levels_button__oiTOw tw-mr-8'>"+
					"<a  value='"+item.car_level_id+"' onclick='changelevel(this)' >"+item.car_level_name+"</a><i class='levels_split__5XIy_'></i>"+
					"</div>"
		});
		level_str+="</div>"
		$("#level_all div").remove();
		$("#level_all").append(level_str);
	},"json");
}

/*************** 根据等级查询 ***************************/
function changelevel(element){
	pageRankingIndex=1;
	load_leng=1500;
	car_level_id=$(element).attr("value");
	$(element).parent().siblings("div").attr("class","tw-relative levels_button__oiTOw tw-mr-8");
	$(element).parent().attr("class","tw-relative levels_button__oiTOw tw-mr-8 levels_active__3usSw levels_large__LbzbP");
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}

function changelevelAll(element){
	pageRankingIndex=1;
	load_leng=1500;
	car_level_id=$(element).attr("value");
	$(element).parent().siblings("div").attr("class","tw-relative levels_button__oiTOw tw-mr-8");
	$(element).parent().attr("class","tw-relative levels_button__oiTOw tw-mr-8 levels_active__3usSw levels_large__LbzbP");
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}

/*************** 根据价格查询 ***************************/
function changepriceAll(element){
	pageRankingIndex=1;
	load_leng=1500;
	min_price=$(element).attr("min");
	max_price=$(element).attr("max");
	$(element).parent().parent().parent().parent().children("span:eq(0)").text("价格");
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}
function changeprice(element){
	pageRankingIndex=1;
	load_leng=1500;
	min_price=$(element).attr("min");
	max_price=$(element).attr("max");
	var price_name=$(element).text();
	$(element).parent().parent().parent().parent().children("span:eq(0)").text(price_name);
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}

/*************** 根据日期查询 ***************************/
function changedateAll(element){
	pageRankingIndex=1;
	load_leng=1500;
	sales_sell_date=$(element).attr("value");
	$(element).parent().parent().parent().parent().children("span:eq(0)").text("日期");
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}
function changedate(element){
	pageRankingIndex=1;
	load_leng=1500;
	sales_sell_date=$(element).attr("value");
	var sales_sell_name=$(element).text();
	$(element).parent().parent().parent().parent().children("span:eq(0)").text(sales_sell_name);
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}
function changeOneYear(element){
	pageRankingIndex=1;
	load_leng=1500;
	sales_sell_date=$(element).attr("value");
	var sales_sell_name=$(element).text();
	$(element).parent().parent().parent().parent().children("span:eq(0)").text(sales_sell_name);
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}
function change_half_a_year(element){
	pageRankingIndex=1;
	load_leng=1500;
	sales_sell_date=$(element).attr("value");
	var sales_sell_name=$(element).text();
	$(element).parent().parent().parent().parent().children("span:eq(0)").text(sales_sell_name);
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}

/*************** 循环日期查询 ***************************/
function timeAll(){
	$.post(path+'Sales/timeAll',function(data){
		var sales_str="";
		sales_str+="<a class='more_option___6xpL' value='0' onclick='changedateAll(this)'>不限日期</a>"
		$(data).each(function(index,item){
			sales_str+="<a class='more_option___6xpL' value='"+item.newtime+"' onclick='changedate(this)' >"+item.newtime+"</a>"
		});
		sales_str+="<a class='more_option___6xpL' value='500' onclick='change_half_a_year(this)' >近半年</a>"+
			"<a class='more_option___6xpL' value='1000' onclick='changeOneYear(this)'>近一年</a>"
		$("#salesDate a").remove();
		$("#salesDate").append(sales_str);
	},"json");
}


//************** 根据品牌查询 **************************
function changebrand(element){
	pageRankingIndex=1;
	load_leng=1500;
	brand_id=$(element).attr("value");
	var brand_name=$(element).children().text();
	$(element).parent().parent().parent().parent().parent().children("span:eq(0)").text(brand_name);
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}
function changebrandAll(element){
	pageRankingIndex=1;
	load_leng=1500;
	brand_id =$(element).attr("value");
	$(element).parent().parent().parent().parent().parent().children("span:eq(0)").text("品牌");
	car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
}

//************** 排行榜查询所有的品牌 **************************
function frist_letter_brand_all(){
		$.post(path+'BrandController/select_brandAll',function(data){
			var all_select_brandAll="";  //查询所有品牌
			all_select_brandAll+="<div class='jsx-2401233222 brand-list' id='brand_all' style='position:relative;left:10px;bottom:0px;margin-top:10px;'>"+
							"<a  rel='opener' value='0' onclick='changebrandAll(this)'>"+
								"<p class='jsx-2401233222 brand'>全部品牌</p>"+
							"</a>"
				$(data).each(function(index2,item2){
					all_select_brandAll+="<a  rel='opener' value='"+item2.brand_id+"'  onclick='changebrand(this)'>"+
						"<p class='jsx-2401233222 brand'>"+item2.brand_name+"</p>"+
					"</a>"
			});
			all_select_brandAll+="</div>";
			$("#brand_all").remove();
			$("#car_ranking").append(all_select_brandAll);
		},"json");
}

/* 排行榜查询所有(根据销量排行) brand_id,car_level_id,sales_sell_date,min_price,max_price*/
/* $.post(path+'Car_seriesController/select_sales_all_series/'+pageRankingIndex+"/"+brand_id+"/"+car_level_id+"/"+sales_sell_date+"/"+min_price+"/"+max_price,function(data){ */
function car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price){
	$.post(path+'Car_seriesController/select_sales_all_series/'+pageRankingIndex+"/"+brand_id+"/"+car_level_id+"/"+sales_sell_date+"/"+min_price+"/"+max_price,function(data){  
		var sales_all_series ="";  //查询所有的销量
		var information_id;
		pages=data.select_sales_all_series.Pages;  //总页数
			$(data.select_sales_all_series.rankingList).each(function(index,item){
			if(item.information==null){
				information_id=0;
			}else{
				information_id=item.information.information_id;
			}
			sales_all_series+="<li class='list_item__3gOKl' onclick='li_forward(this)'>"+
							"<div class='list_index__3g4js'>"
							if(((pageRankingIndex*10+index+1)-10)==1){
								sales_all_series+="<div class='jsx-2889483494 tw-w-36 tw-h-28'></div>"+
									"<div class='tw-text-color-red-500 tw-font-bold'>"+
										"<i class='DCD_Icon icon_rising tw-align-bottom'></i>2</div>"
							}else if(((pageRankingIndex*10+index+1)-10)==2){
								sales_all_series+="<div class='jsx-1790823732 tw-w-36 tw-h-28'></div>"+
									"-"
							}else if(((pageRankingIndex*10+index+1)-10)==3){
								sales_all_series+="<div class='jsx-4277846353 tw-w-36 tw-h-28'></div>"+
									"<div class='tw-text-common-green tw-font-bold'>"+
										"<i class='DCD_Icon icon_reduce tw-align-bottom'></i>6</div>"
							}else{
								sales_all_series+="<span class='tw-font-DDINExp tw-text-24 tw-font-semibold'>"+((pageRankingIndex*10+index+1)-10)+"</span>"
								if(index%2==0){
									sales_all_series+="<div class='tw-text-color-red-500 tw-font-bold'>"+
										"<i class='DCD_Icon icon_rising tw-align-bottom'></i>"+index+"</div>"
								}else{
									sales_all_series+="<div class='tw-text-common-green tw-font-bold'>"+
										"<i class='DCD_Icon icon_reduce tw-align-bottom'></i>"+index+"</div>"
								}
									
							}
							sales_all_series+="</div>"+
							"<div class='tw-p-12'>"+
								"<div class='tw-relative tw-h-80'>"+
									"<div style='display:block;overflow:hidden;position:absolute;top:0;left:0;bottom:0;right:0;z-index:0'>"+
										"<noscript>"+
										"<img src='"+imgs+item.car_series_images_url_1+"' alt='"+item.car_series_name+"' width='180' "+
										"height='80' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:contain' class='view-image_zoom__3b9VV' />"+
										"</noscript>"+
										"<img src='"+imgs+item.car_series_images_url_1+"' alt='"+item.car_series_name+"' width='180' height='80' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:contain' class='zoom' />"+
									"</div>"+
								"</div>"+
							"</div>"+
							"<div class='tw-py-16 tw-pr-12'>"+
								"<div class='tw-leading-28 tw-h-28 tw-truncate'>"+
								"<a href='"+pathHtml+"car_series_index.html?"+item.car_series_id+"' target='_blank' class='tw-font-semibold'>"+item.car_series_name+"</a>"+
								"</div>"+
								"<p class='tw-leading-22 tw-text-color-red-500 tw-font-semibold'>"+
								" "+item.min_manufacturer_price+"-"+item.max_manufacturer_price+"万</p>"+
								"<div class='list_btn-wrap__307w8'>"+
								"<a href='"+pathHtml+"car_series_video.html?"+item.car_series_id+"' target='_blank'>视频</a>"+
								"<span></span>"+
								"<a href='"+pathHtml+"car_actual_large_picture.html?0/"+item.car_series_id+"' target='_blank'>图片</a>"+
								"<span></span>"+
								"<a href='"+pathHtml+"car_series_article.html?"+item.car_series_id+"' target='_blank'>资讯</a>"+
								"</div>"+
							"</div>"+
							"<div class='tw-py-16 tw-text-center'>"+
								"<div class='tw-inline-block tw-text-left tw-min-w-120'>"+
									"<p class='tw-text-18 tw-font-semibold tw-leading-28'>"+commaSeparateNumber(item.sales.sales_count)+"</p>"+
									
								"</div>"+
							"</div>"+
							"<div class='tw-pt-28'>"+
							"<a target='_blank' href='"+pathHtml+"car_purchase_calculation.html?"+item.car_series_id+"/0"+"' "+
							" class='list_button__17SFf list_price__2EMWK' rel='opener nofollow'>购车计算</a>"+
							"</div>"+
						"</li>"
						
		});
		if(pageRankingIndex==1){
			$("#sales_ranking li").remove();
		}
		$("#sales_ranking").append(sales_all_series);
	},"json");
}

/* li跳转路径 */
function li_forward(element){
	$(element).location.href=pathHtml+"car_series_index.html?"+car_series_id;
}

/* 销售量每三位一个逗号 */
 function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
    }
    return val;
  }
  
/* 懒加载刷新页面 */
window.onscroll = function() {
	var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
	var win_top = window.innerHeight;
	if (scroll_top + win_top > load_leng && pageRankingIndex != pages) {
		pageRankingIndex++;
		car_series_ranking_all(brand_id,car_level_id,sales_sell_date,min_price,max_price);
		load_leng += 1000;
	}
}
  
  
/* 品牌展开/收起 */
function more_space_car_series(element) {
	$(element).children("div:eq(0)").show();
	$(element).mouseenter(function(){
		$(element).children("div:eq(0)").show();
	});
	$(element).mouseleave(function(){
		$(element).children("div:eq(0)").hide();
	});
}

/* 价格展开/收起  */
function price_open_close(element){
	//$(element).children("div:eq(0)").toggle();
	$(element).children("div:eq(0)").show();
	$(element).mouseenter(function(){
		$(element).children("div:eq(0)").show();
	});
	$(element).mouseleave(function(){
		$(element).children("div:eq(0)").hide();
	});
}

/* 日期展开/收起 */
function date_open_close(element){
	//$(element).children("div:eq(0)").toggle();
	$(element).children("div:eq(0)").show();
	$(element).mouseenter(function(){
		$(element).children("div:eq(0)").show();
	});
	$(element).mouseleave(function(){
		$(element).children("div:eq(0)").hide();
	});
}

// /* 销量趋势展开/收起 */
// function sales_trend(){
// 	//$(element).children("div:eq(0)").toggle();
// 	var aa =$(this).children().attr("class","DCD_Icon tw-align-bottom tw-ml-2 icon_triangle_up_12");
// 	alert(aa);
// 	$(this).children("span").text().replace("收起销量趋势");
// 	//$(element).children("span").text("");
// }


/* 回到顶点 */
function backVertex(windowTop){
	//向上移动到0px
	$("#gotoTop").click(function(){
		$('html,body').animate({scrollTop:'0px'},'slow');
	});
	
	//获取最小高度，无法入值则默认600
	windowTop?windowTop=windowTop:windowTop=600;
	
	//为窗口的scroll事件绑定函数
	$(window).scroll(function(){
		//获取当前高度 
		var height=$(window).scrollTop();
		if(height>windowTop){
			$("#gotoTop").fadeIn(500);//500毫秒速度
		}else{
			$("#gotoTop").fadeOut(500);
		}
	})
}

/* 显示正在加载 */
function AddRunningDiv() {
            $("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: "100%", height: $(document).height() }).appendTo("#sales_ranking");
            $("<div class=\"datagrid-mask-msg\"></div>").html("正在处理，请稍候...").appendTo("#sales_ranking").css({ display: "block", left: ($(document.body).outerWidth(true) - 190) / 2, top: ($(document).height() - 380)});
} 
	
/* 移除正在加载 */	
function MoveRunningDiv() {
	$("div[class='datagrid-mask']").remove();
	$("div[class='datagrid-mask-msg']").remove();
}  

